package la.iok.hzsvn.lewin.movie.service;

import la.iok.hzsvn.lewin.movie.core.RoleEnum;
import la.iok.hzsvn.lewin.movie.core.Token;
import la.iok.hzsvn.lewin.movie.core.exception.ErrorCode;
import la.iok.hzsvn.lewin.movie.entity.HolidayConfig;
import la.iok.hzsvn.lewin.movie.mapper.HolidayConfigMapper;
import la.iok.hzsvn.lewin.mybatis.service.BaseServiceImpl;
import la.iok.hzsvn.share.annotations.NotNull;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.format.DateTimeParseException;
import java.util.Collections;
import java.util.List;

@Service
public class HolidayConfigServiceImpl extends BaseServiceImpl<HolidayConfig, HolidayConfigMapper, Long> implements HolidayConfigService {
    public HolidayConfigServiceImpl(HolidayConfigMapper mapper) {
        super(mapper);
    }

    @Override
    public Long save(@NotNull Token token, String date, Integer type) {
        try{
            token.validAnyAuthority(RoleEnum.Admin);
            ErrorCode.PROPERTY_CHECK_ERROR.assertAnyOf(type, new Integer[]{0, 1},"类型","只能是0(工作日)或者1(假期)");
            LocalDate localDate = LocalDate.parse(date);
            HolidayConfig entity = select(localDate);
            if(entity == null){
                entity = new HolidayConfig();
                entity.setDate(localDate);
                entity.setType(type);
                insertSelective(entity);
            }else{
                update(entity.getId(), "type", type);
            }
            return entity.getId();
        }catch (DateTimeParseException e){
            ErrorCode.PROPERTY_CHECK_ERROR.fail("日期", "日期格式必须是yyyy-MM-dd形式", e);
        }
        return null;
    }

    @Override
    public List<HolidayConfig> list(Token token, String time) {
        token.validAnyAuthority(RoleEnum.Admin);
        return list(time);
    }

    @Override
    public List<HolidayConfig> list(String time) {
        time = ErrorCode.PROPERTY_CHECK_ERROR.assertNotBlank(time,"日期","格式：yyyy或者yyyy-MM或者yyyy-MM-dd");
        ErrorCode.PROPERTY_CHECK_ERROR.assertAnyOf(time.length(),new Integer[]{4, 7, 10},"日期","格式：yyyy或者yyyy-MM或者yyyy-MM-dd");
        if(time.length() == 4){
            //查询年。9999年以内可以认为长度为4就是查询年
            return list(time+"-01-01", time+"-12-31");
        }else if(time.length() == 7) {
            //查月份
            LocalDate s = null, e = null;
            try{
                s = LocalDate.parse(time+"-01");
                e = s.plusDays(s.lengthOfMonth()-1);
            }catch (DateTimeParseException ex){
                ErrorCode.PROPERTY_CHECK_ERROR.fail("日期", "查询月份的日期格式必须是yyyy-MM形式", ex);
            }
            return mapper.range(s,e);
        }else{
            return Collections.singletonList(select(time));
        }
    }

    @Override
    public HolidayConfig select(String date) {
        LocalDate d = null;
        try{
            d = LocalDate.parse(date);
        }catch (DateTimeParseException e){
            ErrorCode.PROPERTY_CHECK_ERROR.fail("日期", "日期格式必须是yyyy-MM-dd形式", e);
        }
        return select(d);
    }

    private List<HolidayConfig> list(String start, String end){
        LocalDate s = null, e = null;
        try{
            s = LocalDate.parse(start);
            e = LocalDate.parse(end);
        }catch (DateTimeParseException ex){
            ErrorCode.PROPERTY_CHECK_ERROR.fail("日期", "日期格式必须是yyyy-MM-dd形式", ex);
        }
        return mapper.range(s, e);
    }

    private HolidayConfig select(@NotNull LocalDate date){
        return select("date", date, "id desc limit 1");
    }
}
